package us.codecraft.demo.pipeline;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import us.codecraft.demo.model.BaoZou;
import us.codecraft.demo.service.BaoZouService;
import us.codecraft.demo.service.FundService;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class BaoZouProcessor implements PageProcessor {
	private ApplicationContext context;
	
	
	
	private Site site = Site.me().setRetryTimes(3).setSleepTime(100);  
	
	public Site getSite() {
		return site;
	}
	
	public BaoZouProcessor() {
        context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    }
	
	@Autowired
    private BaoZouService baoZouService;
	
	public void process(Page page) {
		//获取分页信息链接并放入到队列中
		page.addTargetRequests(page.getHtml().css("div.pager-content").links().all());  
        BaoZou news = new BaoZou();  
        
        //利用xpath获取所需信息
        news.setAuthor(page.getHtml().xpath("//a[@class='article-author-name']/text()").toString());  
        news.setContent(page.getHtml().xpath("//div[@class='article article-text']/@data-text").toString());  
        news.setTime(page.getHtml().xpath("//span[@class='article-date']/text()").toString());  
        System.out.println("作者:" +news.getAuthor());
        System.out.println("时间:" + news.getTime());
        System.out.println("内容:" +news.getContent() );
        baoZouService = (BaoZouService) context.getBean("baoZouService");
        page.putField("news", news);  
        int result = baoZouService.insert(news);
        System.out.println(result);

        System.out.println("-------") ;
	}

	public static void main(String[] args) {
		Spider.create(new BaoZouProcessor())  
        // 从"http://baozoumanhua.com/text"开始抓  
        .addUrl("http://baozoumanhua.com/text")  
        // 开启5个线程抓取  
        .thread(5)  
        // 启动爬虫  
        .run();  
	}

}
